/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package sza.oracle.apps.ap.oie.traveExp.webui;

import com.sun.java.util.collections.HashMap;

import java.io.Serializable;

import java.sql.CallableStatement;
import java.sql.SQLException;

import oracle.apps.ap.oie.entry.webui.EntryFlowPageCO;
import oracle.apps.ap.oie.webui.NavigationUtility;
import oracle.apps.fnd.common.MessageToken;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import oracle.apps.fnd.framework.webui.beans.layout.OAHeaderBean;

import oracle.core.ojdl.LogMessage;

import oracle.jbo.Row;
import oracle.jbo.ViewObject;

import oracle.jbo.server.DBTransaction;

import oracle.jdbc.OracleCallableStatement;

import sza.oracle.apps.ap.oie.server.ExpenseTypeHierarchyVOImpl;
import sza.oracle.apps.ap.oie.utility.SzaOIEUtil;

/**
 * Controller for ...
 */
public class TravelDetailCO extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = 
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);
       
        String reportHeaderId = 
            pageContext.getSessionValue("ReportHeaderId") + "";
        OAApplicationModule am = 
            (OAApplicationModule)pageContext.getApplicationModule(webBean);
    
            Serializable[] parameters = { reportHeaderId };
            am.invokeMethod("initExpInfo", parameters);
        
    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, 
                                   OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);
        OAApplicationModule am = 
            (OAApplicationModule)pageContext.getApplicationModule(webBean);

        String reportHeaderId = 
            pageContext.getSessionValue("ReportHeaderId") + "";
        Serializable[] parameters = { reportHeaderId };
        if (pageContext.getParameter("AddBtn") != null) {
            am.invokeMethod("addTravelRow", parameters);
        } else if (pageContext.getParameter("addExp") != null) {
            am.invokeMethod("addExpRow", parameters);
        }else if("save".equals(pageContext.getParameter("event"))){
           am.getTransaction().commit();
           //做系统校验
            String strSql = " BEGIN SZA_OIE_TRAVEL_PKG.main(errbuf => :1,retcode => :2,p_report_header_id => :3); END; ";
            DBTransaction trans = am.getOADBTransaction();
            String returnResultErrbuf = "";
            String returnResultRetcode = "";
            CallableStatement cs = trans.createCallableStatement(strSql, 1);
            try
            {
             
              cs.setString(3, reportHeaderId);
              ((OracleCallableStatement)cs).registerOutParameter(1, 12, 0, 2000);
              ((OracleCallableStatement)cs).registerOutParameter(2, 12, 0, 2000);
              cs.execute();
              returnResultErrbuf = cs.getString(1);
              returnResultRetcode = cs.getString(2);
              cs.close();
            }
            catch (SQLException sqle)
            {
             
              try
              {
                cs.close();
              }
              catch (Exception e)
              {
              }
            
             
            }
            if ("1".equals(returnResultRetcode))
            {
             
              String strConfrimMessage = returnResultErrbuf;
              OAException confirmMessage = new OAException(strConfrimMessage, (byte)0);
            
              pageContext.putDialogMessage(confirmMessage);
              return;
            }
           //校验完成
           //更新费用行信息
           System.out.println("saveBtn clicked......");
           OAApplicationModule rootAm = pageContext.getRootApplicationModule();
           OAViewObject lineVo = (OAViewObject)rootAm.findViewObject("CashAndOtherLinesVO");
           OAViewObject expenseVo = (OAViewObject)am.findViewObject("SzaOieExpenseDetailTVO1");
           int expenseRowCount = 0;
           if(expenseVo!=null){
               System.out.println("expenseVo is not null and reportHeaderId is:....."+reportHeaderId);
              Row[] expenseRows = expenseVo.getAllRowsInRange();
               expenseRowCount=expenseRows.length;
              System.out.println("expenseRows.length:"+expenseRows.length);
              if(expenseRows.length==0){
                  if(lineVo!=null){
                      Row[] lines = lineVo.getFilteredRows("ReportHeaderId",reportHeaderId);
                      for(int k = 0; k<lines.length;k++){
                          Row line = lines[k];
                          line.remove();
                      }
                  }
              }
              for (int i=0;i<expenseRows.length;i++){
                  Row expenseRow = expenseRows[i];
                  String lineNumber = expenseRow.getAttribute("LineNumber")+"";
                  System.out.println("lineNumber:"+lineNumber);
                  if(lineVo!=null){
                     Row[] lineRows = lineVo.getFilteredRows("ReportHeaderId",reportHeaderId);
                     System.out.println("lineRows.length:"+lineRows.length);
                     for(int j=0;j<lineRows.length;j++){
                         Row lineRow = lineRows[j];
                         String distLineNumber = lineRow.getAttribute("DistributionLineNumber")+"";
                         String strLineNumber = lineRow.getAttribute("StringLineNumber")+"";
                         System.out.println("distLineNumber:"+distLineNumber);
                         System.out.println("StringLineNumber:"+strLineNumber);
                         if(lineNumber.equals(strLineNumber)){
                             System.out.println("lineNumber:"+lineNumber+"  equals:strLineNumber"+strLineNumber);
                             lineRow.setAttribute("WebParameterId", expenseRow.getAttribute("Attribute9"));
                             lineRow.setAttribute("ReceiptCurrencyCode",expenseRow.getAttribute("Attribute2"));
                             
                             lineRow.setAttribute("ItemDescription", expenseRow.getAttribute("ExpenseCategory"));
                             
                             lineRow.setAttribute("StartExpenseDate",expenseRow.getAttribute("ExpenseDate"));
                             lineRow.setAttribute("StringExpensedAmount",expenseRow.getAttribute("Amount"));
                             lineRow.setAttribute("Justification",expenseRow.getAttribute("ExpenseDesc"));
                             
                             lineRow.setAttribute("Attribute10", expenseRow.getAttribute("Attribute10"));
                             
                             lineRow.setAttribute("Attribute11", expenseRow.getAttribute("Attribute1"));
                             
                             lineRow.setAttribute("Attribute12", expenseRow.getAttribute("Attribute12"));
                             
                             lineRow.setAttribute("Attribute13", expenseRow.getAttribute("Attribute13"));
                             
                             lineRow.setAttribute("Attribute14", expenseRow.getAttribute("Attribute14"));
                             
                             lineRow.setAttribute("Attribute15", expenseRow.getAttribute("Attribute15"));
                         }
                         if(new Integer(strLineNumber).intValue()>expenseRowCount){
                             System.out.println("StringLineNumber:"+strLineNumber +" will be reset to null!");
                             lineRow.remove();

                         }
                     }
                  }
              }
              
              
               
           }
        }  else if ("selectExpType".equals(pageContext.getParameter("event"))) {
            String expenseDetailId = 
                pageContext.getParameter("expDetailId") + "";
            Serializable[] expParams = { reportHeaderId,expenseDetailId };
            am.invokeMethod("deleteExp",expParams);
           
        } else if("deleteTravel".equals(pageContext.getParameter("event"))){
            String travelDetailId = pageContext.getParameter("travelDetailId")+"";
            Serializable[] traParams = { reportHeaderId,travelDetailId };
            am.invokeMethod("deleteTra",traParams);  
        } else if("addTravel".equals(pageContext.getParameter("event"))){
            am.invokeMethod("addTravelRow", parameters);
            
        } else if("addExp".equals(pageContext.getParameter("event"))){
            am.invokeMethod("addExpRow", parameters);
            
        }
    }
    


}
